%define debug_package %{nil}

%ifarch %{x86_64}
%global secondary_distarch i686
%endif

Name:		mos-repos
Version: 	%{rosa_release}
# During devel, it should be 0.0.X
# During release candidate, it should be 0.1.X
# Before final release, bump to 1
Release:	5
Summary:	ROSA-based M OS package repositories
Group:		System/Base
License:	MIT

# ROSA GPG key
Source0:	RPM-GPG-KEY-ROSA

# ROSA release repo config templates
Source2:	rosa-extrasect-repo
Source3:	rosa-main-srcrepo
Source4:	rosa-extrasect-srcrepo
Source5:	rosa-extrasect-repo-testing
Source6:	rosa-extrasect-srcrepo-testing

# MOS repository with packages for ROSA
# (affectively equal to main, not contrib)
Source7:	mos-extrasect-repo
Source8:	mos-extrasect-repo-testing

Source9:	README.MOS

Provides:	mos-repos(%{version})
Provides:	rosa-repos(%{version})
Requires:	distro-release
Requires:	mos-repos-pkgprefs = %{EVRD}
Requires:	mos-repos-keys = %{EVRD}

Requires:	mos-repos-main
Recommends:	mos-repos-pkgprefs
Recommends:	mos-repos-contrib
Recommends:	mos-repos-non-free

%description
Configuration of ROSA-based MOS main repositories

%files
%doc README.MOS
# empty, meta-package

#--------------------------------------------------------------------------------------------

%package testing
Summary:	ROSA-based MOS testing package repositories meta-package
Group:		System/Base
Requires:	(mos-repos-main-testing if mos-repos-main)
Requires:	(mos-repos-contrib-testing if mos-repos-contrib)
Requires:	(mos-repos-non-free-testing if mos-repos-non-free)
Provides:	rosa-repos-testing = %{EVRD}

%description testing
ROSA-based MOS testing package repositories meta-package.
Install it to enable all testing repos.

%files testing
# empty, meta-package

#--------------------------------------------------------------------------------------------

%package main
Summary:	ROSA-based MOS package repositories - Main
Group:		System/Base

%description main
Configuration for ROSA-based MOS Main repositories

%files main
%{_sysconfdir}/yum.repos.d/*rosa-mirror*main*.repo
%{_sysconfdir}/yum.repos.d/*mos*main*.repo
%exclude %{_sysconfdir}/yum.repos.d/*rosa-mirror*main*testing*.repo
%exclude %{_sysconfdir}/yum.repos.d/*mos*main*testing*.repo

#--------------------------------------------------------------------------------------------

%package main-testing
Summary:	ROSA-based MOS package repositories - Main Testing
Group:		System/Base
Requires:	mos-repos-main = %{EVRD}
Provides:	rosa-repos-main-testing = %{EVRD}

%description main-testing
Conficuration for ROSA-based MOS Main Testing repositories

%files main-testing
%config(noreplace) %{_sysconfdir}/yum.repos.d/*rosa-mirror*main*testing*.repo
%config(noreplace) %{_sysconfdir}/yum.repos.d/*mos*main*testing*.repo

#--------------------------------------------------------------------------------------------

%package contrib
Summary:	ROSA package repositories - Contrib
Group:		System/Base
Requires:	mos-repos-main = %{EVRD}
Provides:	rosa-repos-contrib = %{EVRD}

%description contrib
Configuration of ROSA contrib (community-supported) repository

%files contrib
%config(noreplace) %{_sysconfdir}/yum.repos.d/*rosa-mirror*contrib*.repo
%exclude %{_sysconfdir}/yum.repos.d/*rosa-mirror*contrib*testing*.repo

#--------------------------------------------------------------------------------------------

%package contrib-testing
Summary:	ROSA package repositories - Contrib Testing
Group:		System/Base
Requires:	mos-repos-contrib = %{EVRD}
Provides:	rosa-repos-contrib-testing = %{EVRD}

%description contrib-testing
Configuration of ROSA Contrib (community-supported) Testing repository

%files contrib-testing
%config(noreplace) %{_sysconfdir}/yum.repos.d/*rosa-mirror*contrib*testing*.repo

#--------------------------------------------------------------------------------------------

%package non-free
Summary:	ROSA package repositories - non-free
Group:		System/Base
Requires:	mos-repos-main = %{EVRD}
Provides:	rosa-repos-non-free = %{EVRD}

%description non-free
Configuration of ROSA non-free repository

%files non-free
%config(noreplace) %{_sysconfdir}/yum.repos.d/*rosa-mirror*non-free*.repo
%config(noreplace) %{_sysconfdir}/yum.repos.d/*mos*non-free*.repo
%exclude %{_sysconfdir}/yum.repos.d/*rosa-mirror*non-free*testing*.repo
%exclude %{_sysconfdir}/yum.repos.d/*mos*non-free*testing*.repo

#--------------------------------------------------------------------------------------------

%package non-free-testing
Summary:	ROSA package repositories - Non-free Testing
Group:		System/Base
Requires:	mos-repos-non-free = %{EVRD}
Provides:	rosa-repos-non-free-testing = %{EVRD}

%description non-free-testing
Configuration of ROSA Non-free Testing repository

%files non-free-testing
%config(noreplace) %{_sysconfdir}/yum.repos.d/*rosa-mirror*non-free*testing*.repo
%config(noreplace) %{_sysconfdir}/yum.repos.d/*mos*non-free*testing*.repo

#--------------------------------------------------------------------------------------------

%package keys
Summary:	ROSA and MOS repositories GPG keys
Group:		System/Base
# GPG keys are architecture independent
BuildArch:	noarch
Provides:	rosa-repos-keys = %{EVRD}

%description keys
ROSA GPG keys for validating packages from ROSA and MOS repositories by
DNF and PackageKit.

%files keys
%dir %{_sysconfdir}/pki/rpm-gpg
%{_sysconfdir}/pki/rpm-gpg/RPM-GPG-KEY-ROSA
# TODO: add key for MOS-specific repos

#--------------------------------------------------------------------------------------------

%package pkgprefs
# (ngompa): See the following page on why this exists:
# https://fedoraproject.org/wiki/PackagingDrafts/ProvidesPreferences#Distribution_preference
Summary:	ROSA repository package preferences
Group:		System/Base
# Preferences list is architecture independent
BuildArch:	noarch
Provides:	rosa-repos-pkgprefs = %{EVRD}

# Default distribution release
Suggests:	branding-configs-default

## Base packages

# webfetch
Suggests:	curl

# webclient
Suggests:	lynx

# bootloader
Suggests:	grub2

# initrd
Suggests:	dracut

# vim
Suggests:	vim-minimal

# always prefer perl-base over weird packages auto-providing same modules (perl-PathTools, perl-Tk, , perl-PDL...)
Suggests:	perl-base

# libGL.so.1 (also provided by proprietary drivers)
Suggests:	libgl1
Suggests:	lib64gl1

# Prefer openssh-askpass over openssh-askpass-gnome (for keychain)
Suggests:	openssh-askpass

# Python 2.7
Suggests:	python

## Multimedia

# festival-voice
Suggests:	festvox-kallpc16k

# gnome-speech-driver
Suggests:	gnome-speech-driver-espeak

# esound
Suggests:	pulseaudio-esound-compat

# gst-install-plugins-helper
Suggests:	packagekit-gstreamer-plugin

# libbaconvideowidget.so.0 (totem backend)
Suggests:	libbaconvideowidget-gstreamer0
Suggests:	lib64baconvideowidget-gstreamer0

# phonon-backend
Suggests:	phonon-gstreamer

# Prever Jack2 over PipeWire
# but allow to use PipeWire as a drop-in replacements of its libraries
# https://rpm.org/user_doc/boolean_dependencies.html
# Keep in sync with pipewire package!
%define pipewire_pref_gen() %{expand:\
Suggests: (%{mklibname %1 %2}%{_isa} without pipewire) \
Suggests: (%{mklibname %{1}_pipewire %2}%{_isa} if pipewire) \
%ifarch %{x86_64} \
Suggests: (lib%{1}%{2}(x86-32) without pipewire) \
Suggests: (lib%{1}_pipewire%{2}(x86-32) if pipewire) \
%endif \
}
%pipewire_pref_gen jack 0
%pipewire_pref_gen jacknet 0
%pipewire_pref_gen jackserver 0

## Devel

# xemacs-extras provides ctags, prefer simple ctags
Suggests:	ctags

# uClibc-devel provides a few things similar to glibc-devel
Suggests:	glibc-devel

# prefer openssl-devel over libressl-devel
Suggests:	libopenssl-devel
Suggests:	lib64openssl-devel

# prefer gcc over gcc3.3
# (gcc-cpp and gcc-c++ are no more needed, but keeping just in case)
Suggests:	gcc-cpp
Suggests:	gcc
Suggests:	gcc-c++
Suggests:	libstdc++-devel

## Servers

# sendmail-command and mail-server
Suggests:	postfix

# nfs-server
Suggests:	nfs-utils

# webserver
Suggests:	apache

# apache
Suggests:	apache-mpm-prefork

# ftpserver
Suggests:	proftpd

# portmapper
Suggests:	portmap

# syslog-daemon
Suggests:	systemd

# vnc
Suggests:	tigervnc

## Various
# sane (also provided by saned)
Suggests:	sane-backends

# virtual-notification-daemon
Suggests:	notification-daemon

# sgml-tools
# (the other choice is linuxdoc-tools which requires docbook-utils anyway)
Suggests:	docbook-utils

# input method
Suggests:	scim-bridge

# lxde session manager
Suggests:	lxsession-lite

# drupal database storage
Suggests:	drupal-mysqli

# polkit-agent
Suggests:	polkit-gnome

# pinentry
Suggests:	pinentry-qt5

# java
Suggests:	java-1.8.0-openjdk
Suggests:	java-1.8.0-openjdk-devel

# java-plugin
Suggests:	icedtea-web

%description pkgprefs
This package supplies DNF and PackageKit with global
preferences for packages in which multiple options are possible.

%files pkgprefs
# empty

#--------------------------------------------------------------------------------------------

%prep
%setup -T -c
cp %{SOURCE9} .

%build
# Nothing to build

%install
ARCH=%{_target_cpu}
echo $ARCH |grep -q arm && ARCH=armv7hl
[ "$ARCH" = "i386" ] && ARCH=i586
[ "$ARCH" = "i586" ] && ARCH=i586

# Install the GPG key
mkdir -p %{buildroot}%{_sysconfdir}/pki/rpm-gpg
install %{S:0} -pm 0644 %{buildroot}%{_sysconfdir}/pki/rpm-gpg

# Install the repositories
mkdir -p %{buildroot}%{_sysconfdir}/yum.repos.d

## Create the repositories for various sections
install %{S:3} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-main-${ARCH}-source.repo
install %{S:4} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-non-free-${ARCH}-source.repo
install %{S:6} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-non-free-testing-${ARCH}-source.repo
install %{S:4} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-contrib-${ARCH}-source.repo
install %{S:6} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-contrib-testing-${ARCH}-source.repo
install %{S:2} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-main-${ARCH}.repo
install %{S:5} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-main-testing-${ARCH}.repo
install %{S:2} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-non-free-${ARCH}.repo
install %{S:5} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-non-free-testing-${ARCH}.repo
install %{S:2} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-contrib-${ARCH}.repo
install %{S:5} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-rosa-mirror-contrib-testing-${ARCH}.repo

# MOS-specific repos
install %{S:7} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-mos-main-${ARCH}.repo
install %{S:7} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-mos-non-free-${ARCH}.repo
install %{S:8} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-mos-main-testing-${ARCH}.repo
install %{S:8} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/10-mos-non-free-testing-${ARCH}.repo

## Fill in the correct values for the installed repo files
sed -e "s/@DIST_ARCH@/${ARCH}/g" -i %{buildroot}%{_sysconfdir}/yum.repos.d/*${ARCH}*.repo

sed -e "s/@DIST_SECTION@/main/g" \
    -e "s/@DIST_SECTION_NAME@/Main/g" \
    -i %{buildroot}%{_sysconfdir}/yum.repos.d/*main*${ARCH}*.repo

sed -e "s/@DIST_SECTION@/non-free/g" \
    -e "s/@DIST_SECTION_NAME@/Non-free/g" \
    -i %{buildroot}%{_sysconfdir}/yum.repos.d/*non-free*${ARCH}*.repo

sed -e "s/@DIST_SECTION@/contrib/g" \
    -e "s/@DIST_SECTION_NAME@/Contrib/g" \
    -i %{buildroot}%{_sysconfdir}/yum.repos.d/*contrib*${ARCH}*.repo

## For architectures with a secondary arch, we need to create repositories for them, too
%if %{defined secondary_distarch}
### Create the repositories for various sections, excluding sources (as they are identical to primary arch ones)
install %{S:2} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-rosa-mirror-main-%{secondary_distarch}.repo
install %{S:5} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-rosa-mirror-main-testing-%{secondary_distarch}.repo
install %{S:2} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-rosa-mirror-non-free-%{secondary_distarch}.repo
install %{S:5} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-rosa-mirror-non-free-testing-%{secondary_distarch}.repo
install %{S:2} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-rosa-mirror-contrib-%{secondary_distarch}.repo
install %{S:5} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-rosa-mirror-contrib-testing-%{secondary_distarch}.repo

# MOS-specific repos
install %{S:7} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-mos-main-%{secondary_distarch}.repo
install %{S:8} -pm 0644 %{buildroot}%{_sysconfdir}/yum.repos.d/20-mos-main-testing-%{secondary_distarch}.repo

### Fill in the correct values for the installed repo files
sed -e "s/@DIST_ARCH@/%{secondary_distarch}/g" -i %{buildroot}%{_sysconfdir}/yum.repos.d/*%{secondary_distarch}*.repo

sed -e "s/@DIST_SECTION@/main/g" \
    -e "s/@DIST_SECTION_NAME@/Main/g" \
    -i %{buildroot}%{_sysconfdir}/yum.repos.d/*main*%{secondary_distarch}*.repo

sed -e "s/@DIST_SECTION@/non-free/g" \
    -e "s/@DIST_SECTION_NAME@/Non-free/g" \
    -i %{buildroot}%{_sysconfdir}/yum.repos.d/*non-free*%{secondary_distarch}*.repo

sed -e "s/@DIST_SECTION@/contrib/g" \
    -e "s/@DIST_SECTION_NAME@/Contrib/g" \
    -i %{buildroot}%{_sysconfdir}/yum.repos.d/*contrib*%{secondary_distarch}*.repo

# install noarch packages from x86_64, not i686
# Lower number means higher priority
find %{buildroot}%{_sysconfdir}/yum.repos.d/ -type f | grep -v "%{secondary_distarch}" | while read -r line
do
	sed -i'' -r -e 's,^cost=.+,cost=999,' "$line"
done

%endif
